package org.tis.tools.abf.module.ac.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorBatchAddRequest;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorFuncQueConditionRequest;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorFuncRequest;
import org.tis.tools.abf.module.ac.controller.request.AcRoleFuncQueRequest;
import org.tis.tools.abf.module.ac.dao.AcOperatorFuncMapper;
import org.tis.tools.abf.module.ac.entity.*;
import org.tis.tools.abf.module.ac.entity.enums.AcAuthType;
import org.tis.tools.abf.module.ac.entity.enums.FuncType;
import org.tis.tools.abf.module.ac.entity.vo.AcAppRoleDetail;
import org.tis.tools.abf.module.ac.entity.vo.AcFuncExit;
import org.tis.tools.abf.module.ac.entity.vo.AcOpertorAddDelete;
import org.tis.tools.abf.module.ac.entity.vo.OperatorRoleAdd;
import org.tis.tools.abf.module.ac.exception.AcExceptionCodes;
import org.tis.tools.abf.module.ac.exception.AcManagementException;
import org.tis.tools.abf.module.ac.service.*;
import org.tis.tools.core.utils.StringUtil;

import java.util.*;

import static org.tis.tools.core.utils.BasicUtil.wrap;

/**
 * acOperatorFunc的Service接口实现类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AcOperatorFuncServiceImpl extends ServiceImpl<AcOperatorFuncMapper, AcOperatorFunc> implements IAcOperatorFuncService {

    @Autowired
    private IAcOperatorService acOperatorService;
    @Autowired
    private IAcFuncService acFuncService;
    @Autowired
    private IAcAppService acAppService;
    @Autowired
    private IAcOperatorRoleService acOperatorRoleService;
    @Autowired
    private IAcRoleFuncService acRoleFuncService;

    @Override
    public void add(AcOperatorFuncRequest acOperatorFuncRequestc) throws AcManagementException {

        //判断操作员是否存在
        String operatorGuid = acOperatorFuncRequestc.getGuidOperator();
        AcOperator acOperator = acOperatorService.selectById(operatorGuid);
        if (acOperator == null){
            throw new AcManagementException(AcExceptionCodes.FAILURE_WHEN_QUERY_AC_OPERATOR,wrap("操作员ID对应的操作员不存在",
                    operatorGuid));
        }

        //判断功能是否存在
        String funcGuid = acOperatorFuncRequestc.getGuidFunc();
        AcFunc acFunc = acFuncService.selectById(funcGuid);
        if (acFunc == null){
            throw new AcManagementException(AcExceptionCodes.FAILURE_WHRN_QUERY_AC_FUNC,wrap("功能ID对应的功能不存在",
                    funcGuid));
        }

        //判断应用是否存在
        String appGuid = acOperatorFuncRequestc.getGuidApp();
        if (!StringUtil.isEmpty(appGuid)){
            AcApp acApp = acAppService.selectById(appGuid);
            if (acApp == null){
                throw new AcManagementException(AcExceptionCodes.FAILURE_WHRN_QUERY_AC_APP,wrap("应用ID对应的应用不存在",
                        appGuid));
            }
        }

        //收集参数
        AcOperatorFunc acOperatorFunc = new AcOperatorFunc();
        acOperatorFunc.setGuidFunc(funcGuid);
        acOperatorFunc.setGuidOperator(operatorGuid);
        acOperatorFunc.setGuidApp(appGuid);
        acOperatorFunc.setAuthType(acOperatorFuncRequestc.getAuthType());
        acOperatorFunc.setStartDate(acOperatorFuncRequestc.getStartDate());
        acOperatorFunc.setEndDate(acOperatorFuncRequestc.getEndDate());

        //新增
        insert(acOperatorFunc);
    }


    @Override
    public AcOperatorFunc change(AcOperatorFuncRequest acOperatorFuncRequestc) throws AcManagementException {
        //判断操作员是否存在
        String operatorGuid = acOperatorFuncRequestc.getGuidOperator();
        AcOperator acOperator = acOperatorService.selectById(operatorGuid);
        if (acOperator == null){
            throw new AcManagementException(AcExceptionCodes.FAILURE_WHEN_QUERY_AC_OPERATOR,wrap("操作员ID对应的操作员不存在",
                    operatorGuid));
        }

        //判断功能是否存在
        String funcGuid = acOperatorFuncRequestc.getGuidFunc();
        AcFunc acFunc = acFuncService.selectById(funcGuid);
        if (acFunc == null){
            throw new AcManagementException(AcExceptionCodes.FAILURE_WHRN_QUERY_AC_FUNC,wrap("功能ID对应的功能不存在",
                    funcGuid));
        }

        //判断应用是否存在
        String appGuid = acOperatorFuncRequestc.getGuidApp();
        if (!StringUtil.isEmpty(appGuid)){
            AcApp acApp = acAppService.selectById(appGuid);
            if (acApp == null){
                throw new AcManagementException(AcExceptionCodes.FAILURE_WHRN_QUERY_AC_APP,wrap("应用ID对应的应用不存在",
                        appGuid));
            }
        }

        //收集参数
        AcOperatorFunc acOperatorFunc = new AcOperatorFunc();
        acOperatorFunc.setGuid(acOperatorFuncRequestc.getGuid());
        acOperatorFunc.setGuidFunc(funcGuid);
        acOperatorFunc.setGuidOperator(operatorGuid);
        acOperatorFunc.setGuidApp(appGuid);
        acOperatorFunc.setAuthType(acOperatorFuncRequestc.getAuthType());
        acOperatorFunc.setStartDate(acOperatorFuncRequestc.getStartDate());
        acOperatorFunc.setEndDate(acOperatorFuncRequestc.getEndDate());

        //更新
        updateById(acOperatorFunc);
        return acOperatorFunc;
    }

    /**
     * 查询所有应用和操作员下已有应用
     */
    @Override
    public AcRoleFuncQueRequest<AcApp> queryAppByOperator(String operatorId) throws AcManagementException {

        //查询所有的应用
        Wrapper<AcApp> appWrapper = new EntityWrapper<>();
        List<AcApp> allApps = acAppService.selectList(appWrapper);

        //查询已有应用
        List<AcApp> oldApps = this.baseMapper.queryAppByOperator(operatorId);

        AcRoleFuncQueRequest<AcApp> queRequest = new AcRoleFuncQueRequest<>();
        queRequest.setAllData(allApps);
        queRequest.setOldData(oldApps);

        return queRequest;
    }

    /**
     * 查询应用和操作员下已有的功能和行为 和 所有的功能行为
     * 启用时，角色对应的权限生效，禁用则权限不生效
     * 操作员绑定的角色,有效角色权限集
     */
    @Override
    public List<AcFuncExit> queryFuncBehaveByOperatorApp(String operatorId, String appId, String name) throws
            AcManagementException {

        //返回数据
        List<AcFuncExit> exitList = new ArrayList<>();

//        //查询应用下的所有功能
//        Wrapper<AcFunc> allFuncWrapper = new EntityWrapper<AcFunc>();
//        allFuncWrapper.eq(AcFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
//        allFuncWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.FUNCTION);
//        List<AcFunc> allFuncList = acFuncService.selectList(allFuncWrapper);
//
//        for (AcFunc acFunc : allFuncList){
//            if (null != acFunc){
//                //如果功能存在,获取功能下的所有行为
//                Wrapper<AcFunc> allBehaveWrapper = new EntityWrapper<AcFunc>();
//                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_FUNC,acFunc.getGuid());
//                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
//                allBehaveWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.BEHAVE);
//                List<AcFunc> allBehaveList = acFuncService.selectList(allBehaveWrapper);
//
//                List<AcFunc> assignedBehaveList = this.baseMapper.queryBehaveByOperator(conditionRequest.getOperatorId(),conditionRequest
//                        .getAppId(),acFunc.getGuid());
//
//                //如果该功能下行为非空,并且已分配的行为也非空,获取所有未分配的行为
//                boolean notBlank = (null != allBehaveList || 0 != allBehaveList.size()) && (null != assignedBehaveList || 0 != assignedBehaveList.size());
//                if (notBlank){
//                    allBehaveList.removeAll(assignedBehaveList);
//                }
//
//                //用于存放该功能下的所有行为,并标识出已分配的为"false",为分配的为"true"
//                List<AcFuncExit> exitBehaveList = new ArrayList<AcFuncExit>();
//                for (AcFunc behave: allBehaveList){
//                    if (null != behave){
//                        AcFuncExit acBehaveExit = new AcFuncExit(behave,"true",null);
//                        exitBehaveList.add(acBehaveExit);
//                    }
//                }
//                for (AcFunc behave: assignedBehaveList){
//                    if (null != behave){
//                        AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null);
//                        exitBehaveList.add(acBehaveExit);
//                    }
//                }
//
//                //处理完功能下的行为,处理该功能.判断该功能是否已分配
//                Wrapper<AcOperatorFunc> acOperatorFuncWrapper = new EntityWrapper<AcOperatorFunc>();
//                acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_OPERATOR,conditionRequest.getOperatorId());
//                acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
//                acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_FUNC,acFunc.getGuid());
//
//                AcOperatorFunc acOperatorFunc = selectOne(acOperatorFuncWrapper);
//                AcFuncExit acFuncExit = new AcFuncExit();
//                if (null == acOperatorFunc){
//                    acFuncExit = new AcFuncExit(acFunc,"true",exitBehaveList);
//                }
//                if (null != acOperatorFunc){
//                    acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList);
//                }
//
//                exitList.add(acFuncExit);
//            }
//        }

        //查询应用下的所有功能
        Wrapper<AcFunc> allFuncWrapper = new EntityWrapper<>();
        allFuncWrapper.eq(AcFunc.COLUMN_GUID_APP,appId);
        allFuncWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.FUNCTION);
        List<AcFunc> allFuncList = acFuncService.selectList(allFuncWrapper);

        for (AcFunc acFunc : allFuncList){
            if (null != acFunc){
                //如果功能存在,获取功能下的所有行为,添加模糊匹配功能
                Wrapper<AcFunc> allBehaveWrapper = new EntityWrapper<>();
                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_FUNC,acFunc.getGuid());
                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_APP,appId);
                allBehaveWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.BEHAVE);
                List<AcFunc> allBehaveList = acFuncService.selectList(allBehaveWrapper);

                //查询角色下的行为,添加模糊匹配
                List<AcFunc> roleBehaveList = this.baseMapper.queryBehaveInRole(operatorId,appId,acFunc.getGuid());

                //查询操作员下的行为,添加模糊匹配,特别允许
                List<AcFunc> operatorRemitBehaveList  = this.baseMapper.queryBehaveRemitInOperator(operatorId,appId,acFunc.getGuid());

                //查询操作员下的行为,添加模糊匹配,特别禁止
                List<AcFunc> operatorForbidBehaveList = this.baseMapper.queryBehaveForbidInOperator(operatorId, appId,acFunc.getGuid());


                //去除掉特别允许中为角色行为的数据
                if (0 != operatorRemitBehaveList.size()){
                    operatorRemitBehaveList.removeAll(roleBehaveList);
                }
                //去除角色行为中特别禁止的数据
                if (0 != roleBehaveList.size()){
                    roleBehaveList.removeAll(operatorForbidBehaveList);
                }
                //去除已经被分配的行为,找出所有未被分配的行为
                if (0 != allBehaveList.size()){
                    allBehaveList.removeAll(roleBehaveList);
                    allBehaveList.removeAll(operatorForbidBehaveList);
                    allBehaveList.removeAll(operatorRemitBehaveList);
                }


                /**
                 * 用于存放该功能下的所有行为
                 * exit标识出 已分配的为"false",未分配的为"true"
                 * fromRole标志出 来自角色:Y,未来自角色:N
                 * authType表指出 特别允许:remit,特别禁止:forbid
                 */
                List<AcFuncExit> exitBehaveList = new ArrayList<>();
                if (0 != allBehaveList.size() || 0 != roleBehaveList.size() || 0 != operatorForbidBehaveList.size()
                        || 0 != operatorRemitBehaveList.size() ){
                    for (AcFunc behave: allBehaveList){
                        if (null != behave){
                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"true",null,"N",null);
                            exitBehaveList.add(acBehaveExit);
                        }
                    }
                    for (AcFunc behave: roleBehaveList){
                        if (null != behave){
                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null,"Y",null);
                            exitBehaveList.add(acBehaveExit);
                        }
                    }
                    for (AcFunc behave : operatorForbidBehaveList){
                        if (null != behave){
                            //因为特别禁止的都是来自角色的,所有fromRole是Y
                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null,"Y","forbid");
                            exitBehaveList.add(acBehaveExit);
                        }
                    }
                    for (AcFunc behave : operatorRemitBehaveList){
                        if (null != behave){
                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null,"N","remit");
                            exitBehaveList.add(acBehaveExit);
                        }
                    }
                }else {
                    //如果行为为空,并且功能也不能匹配,则不执行下面的操作
                    if (!StringUtil.isEmpty(name)){
                        Wrapper<AcFunc> acFuncWrapper = new EntityWrapper<AcFunc>();
                        acFuncWrapper.eq(AcFunc.COLUMN_GUID,acFunc.getGuid());
                        AcFunc acFuncQue = acFuncService.selectOne(acFuncWrapper);
                        if (null == acFuncQue){
                            continue;
                        }
                    }
                }

                //判断功能是否来自于角色
                Wrapper<AcOperatorRole> operatorRoleWrapper = new EntityWrapper<>();
                operatorRoleWrapper.eq(AcOperatorRole.COLUMN_GUID_OPERATOR,operatorId);
                List<AcOperatorRole> acOperatorRoles = acOperatorRoleService.selectList(operatorRoleWrapper);
                AcFuncExit acFuncExit = null;
                if (0 != acOperatorRoles.size()){
                    for (AcOperatorRole acOperatorRole : acOperatorRoles){
                        if (null != acOperatorRole){
                            Wrapper<AcRoleFunc> roleFuncWrapper = new EntityWrapper<>();
                            roleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,acOperatorRole.getGuidRole());
                            roleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_APP,appId);
                            roleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_FUNC,acFunc.getGuid());

                            AcRoleFunc acRoleFunc = acRoleFuncService.selectOne(roleFuncWrapper);
                            if (null != acRoleFunc){
                                acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"Y",null);
                            }
                        }
                    }
                }
                if (0 == acOperatorRoles.size() || null == acFuncExit){
                    //处理完功能下的行为,处理该功能.判断该功能是否已分配
                    Wrapper<AcOperatorFunc> acOperatorFuncWrapper = new EntityWrapper<>();
                    acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_OPERATOR,operatorId);
                    acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_APP,appId);
                    acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_FUNC,acFunc.getGuid());

                    AcOperatorFunc acOperatorFunc = selectOne(acOperatorFuncWrapper);
                    if (null == acOperatorFunc){
                        acFuncExit = new AcFuncExit(acFunc,"true",exitBehaveList,"N",null);
                    }
                    if (null != acOperatorFunc){
                        if (acOperatorFunc.getAuthType().toString().equals(AcAuthType.PERMIT.toString())){
                            acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"N","remit");
                        }else {
                            //功能被特别禁止,则功能下的所有行为也被特别禁止
                            for (AcFuncExit acBehaveExit : exitBehaveList){
                                if (null != acBehaveExit){
                                    acBehaveExit.setAuthType("forbid");
                                }
                            }
                            acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"N","forbid");
                        }
                    }
                }
                exitList.add(acFuncExit);
            }
        }

        return exitList;
    }

    /**
     * 根据姓名模糊查询应用和操作员下已有的功能和行为 和 所有的功能行为
     * 启用时，角色对应的权限生效，禁用则权限不生效
     * 操作员绑定的角色,有效角色权限集
     */
    @Override
    public List<AcFuncExit> queryFuncBehaveFilterByOperatorApp(AcOperatorFuncQueConditionRequest conditionRequest)
            throws AcManagementException {

        //返回数据
        List<AcFuncExit> exitList = new ArrayList<>();

        //查询应用下的所有功能
        Wrapper<AcFunc> allFuncWrapper = new EntityWrapper<>();
        allFuncWrapper.eq(AcFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
        allFuncWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.FUNCTION);
        List<AcFunc> allFuncList = acFuncService.selectList(allFuncWrapper);

        for (AcFunc acFunc : allFuncList){
            if (null != acFunc){
                //如果功能存在,获取功能下的所有行为,添加模糊匹配功能
                Wrapper<AcFunc> allBehaveWrapper = new EntityWrapper<>();
                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_FUNC,acFunc.getGuid());
                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
                allBehaveWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.BEHAVE);
                if (!StringUtil.isEmpty(conditionRequest.getName())){
                    allBehaveWrapper.like(AcFunc.COLUMN_FUNC_NAME,conditionRequest.getName());
                }
                List<AcFunc> allBehaveList = acFuncService.selectList(allBehaveWrapper);

                //查询角色下的行为,添加模糊匹配
                List<AcFunc> roleBehaveList = new ArrayList<>();
                if (StringUtil.isEmpty(conditionRequest.getName())){
                    roleBehaveList = this.baseMapper.queryBehaveInRole(conditionRequest.getOperatorId(),conditionRequest
                            .getAppId(),acFunc.getGuid());
                }else {
                    roleBehaveList = this.baseMapper.queryBehaveFilterInRole(conditionRequest.getOperatorId()
                            ,conditionRequest.getAppId(),acFunc.getGuid(),"%"+conditionRequest.getName()+"%");
                }

                //查询操作员下的行为,添加模糊匹配,特别允许
                List<AcFunc> operatorRemitBehaveList = new ArrayList<>();
                if (StringUtil.isEmpty(conditionRequest.getName())){
                    operatorRemitBehaveList = this.baseMapper.queryBehaveRemitInOperator(conditionRequest.getOperatorId(),conditionRequest
                            .getAppId(),acFunc.getGuid());
                }else {
                    operatorRemitBehaveList = this.baseMapper.queryBehaveFilterRemitInOperator(conditionRequest.getOperatorId()
                            ,conditionRequest.getAppId(),acFunc.getGuid(),"%"+conditionRequest.getName()+"%");
                }

                //查询操作员下的行为,添加模糊匹配,特别禁止
                List<AcFunc> operatorForbidBehaveList = new ArrayList<>();
                if (StringUtil.isEmpty(conditionRequest.getName())){
                    operatorForbidBehaveList = this.baseMapper.queryBehaveForbidInOperator(conditionRequest.getOperatorId(),
                            conditionRequest
                            .getAppId(),acFunc.getGuid());
                }else {
                    operatorForbidBehaveList = this.baseMapper.queryBehaveFilterForbidInOperator(conditionRequest.getOperatorId()
                            ,conditionRequest.getAppId(),acFunc.getGuid(),"%"+conditionRequest.getName()+"%");
                }

                //去除掉特别允许中为角色行为的数据
                if (0 != operatorRemitBehaveList.size()){
                    operatorRemitBehaveList.removeAll(roleBehaveList);
                }
                //去除角色行为中特别禁止的数据
                if (0 != roleBehaveList.size()){
                    roleBehaveList.removeAll(operatorForbidBehaveList);
                }
                //去除已经被分配的行为,找出所有未被分配的行为
                if (0 != allBehaveList.size()){
                    allBehaveList.removeAll(roleBehaveList);
                    allBehaveList.removeAll(operatorForbidBehaveList);
                    allBehaveList.removeAll(operatorRemitBehaveList);
                }


                /**
                 * 用于存放该功能下的所有行为
                 * exit标识出 已分配的为"false",未分配的为"true"
                 * fromRole标志出 来自角色:Y,未来自角色:N
                 * authType表指出 特别允许:remit,特别禁止:forbid
                 */
                List<AcFuncExit> exitBehaveList = new ArrayList<>();
                if (0 != allBehaveList.size() || 0 != roleBehaveList.size() || 0 != operatorForbidBehaveList.size()
                        || 0 != operatorRemitBehaveList.size() ){
                    for (AcFunc behave: allBehaveList){
                        if (null != behave){
                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"true",null,"N",null);
                            exitBehaveList.add(acBehaveExit);
                        }
                    }
                    for (AcFunc behave: roleBehaveList){
                        if (null != behave){
                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null,"Y",null);
                            exitBehaveList.add(acBehaveExit);
                        }
                    }
                    for (AcFunc behave : operatorForbidBehaveList){
                        if (null != behave){
                            //特别禁止的都是之前来自于角色的,所有fromRole是Y
                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null,"Y","forbid");
                            exitBehaveList.add(acBehaveExit);
                        }
                    }
                    for (AcFunc behave : operatorRemitBehaveList){
                        if (null != behave){
                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null,"N","remit");
                            exitBehaveList.add(acBehaveExit);
                        }
                    }
                }else {
                    //如果行为为空,并且功能也不能匹配,则不执行下面的操作
                    if (!StringUtil.isEmpty(conditionRequest.getName())){
                        Wrapper<AcFunc> acFuncWrapper = new EntityWrapper<AcFunc>();
                        acFuncWrapper.eq(AcFunc.COLUMN_GUID,acFunc.getGuid());
                        acFuncWrapper.like(AcFunc.COLUMN_FUNC_NAME,conditionRequest.getName());

                        AcFunc acFuncQue = acFuncService.selectOne(acFuncWrapper);
                        if (null == acFuncQue){
                            continue;
                        }
                    }
                }

                //判断功能是否来自于角色
                Wrapper<AcOperatorRole> operatorRoleWrapper = new EntityWrapper<>();
                operatorRoleWrapper.eq(AcOperatorRole.COLUMN_GUID_OPERATOR,conditionRequest.getOperatorId());

                List<AcOperatorRole> acOperatorRoles = acOperatorRoleService.selectList(operatorRoleWrapper);
                AcFuncExit acFuncExit = null;
                if (0 != acOperatorRoles.size()){
                    for (AcOperatorRole acOperatorRole : acOperatorRoles){
                        if (null != acOperatorRole){
                            Wrapper<AcRoleFunc> roleFuncWrapper = new EntityWrapper<>();
                            roleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,acOperatorRole.getGuidRole());
                            roleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
                            roleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_FUNC,acFunc.getGuid());

                            AcRoleFunc acRoleFunc = acRoleFuncService.selectOne(roleFuncWrapper);
                            if (null != acRoleFunc){
                                acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"Y",null);
                            }
                        }
                    }
                }
                if (0 == acOperatorRoles.size() || null == acFuncExit){
                    //处理完功能下的行为,处理该功能.判断该功能是否已分配
                    Wrapper<AcOperatorFunc> acOperatorFuncWrapper = new EntityWrapper<>();
                    acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_OPERATOR,conditionRequest.getOperatorId());
                    acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
                    acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_FUNC,acFunc.getGuid());

                    AcOperatorFunc acOperatorFunc = selectOne(acOperatorFuncWrapper);
                    if (null == acOperatorFunc){
                        acFuncExit = new AcFuncExit(acFunc,"true",exitBehaveList,"N",null);
                    }
                    if (null != acOperatorFunc){
                        if (acOperatorFunc.getAuthType().toString().equals(AcAuthType.PERMIT.toString())){
                            acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"N","remit");
                        }else {
                            //功能被特别禁止,则功能下的所有行为也被特别禁止
                            for (AcFuncExit acBehaveExit : exitBehaveList){
                                if (null != acBehaveExit){
                                    acBehaveExit.setAuthType("forbid");
                                }
                            }
                            acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"N","forbid");
                        }
                    }
                }
                exitList.add(acFuncExit);

//                //如果功能存在,获取功能下的所有行为
//                Wrapper<AcFunc> allBehaveWrapper = new EntityWrapper<>();
//                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_FUNC,acFunc.getGuid());
//                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
//                allBehaveWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.BEHAVE);
//                //模糊匹配功能下的所有行为
//                if (!StringUtil.isEmpty(conditionRequest.getName())){
//                    allBehaveWrapper.like(AcFunc.COLUMN_FUNC_NAME,conditionRequest.getName());
//                }
//                List<AcFunc> allBehaveList = acFuncService.selectList(allBehaveWrapper);
//
//                List<AcFunc> assignedBehaveList = new ArrayList<>();
//                if (StringUtil.isEmpty(conditionRequest.getName())){
//                    assignedBehaveList = this.baseMapper.queryBehaveByOperator(conditionRequest.getOperatorId(),conditionRequest
//                            .getAppId(),acFunc.getGuid());
//                }else {
//                    assignedBehaveList = this.baseMapper.queryBehaveFilterByOperator(conditionRequest.getOperatorId()
//                            ,conditionRequest.getAppId(),acFunc.getGuid(),"%"+conditionRequest.getName()+"%");
//                }
//
//
//                //如果该功能下 行为非空,并且已分配的行为也非空,获取所有未分配的行为
//                boolean notBlank = (0 != allBehaveList.size()) && (0 != assignedBehaveList.size());
//                if (notBlank){
//                    allBehaveList.removeAll(assignedBehaveList);
//                }
//
//                //用于存放该功能下的所有行为,并标识出已分配的为"false",为分配的为"true"
//                List<AcFuncExit> exitBehaveList = new ArrayList<AcFuncExit>();
//                if (0 != allBehaveList.size() || 0 != assignedBehaveList.size()){
//                    for (AcFunc behave: allBehaveList){
//                        if (null != behave){
//                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"true",null);
//                            exitBehaveList.add(acBehaveExit);
//                        }
//                    }
//                    for (AcFunc behave: assignedBehaveList){
//                        if (null != behave){
//                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null);
//                            exitBehaveList.add(acBehaveExit);
//                        }
//                    }
//                }else {
//                    //如果行为为空,并且功能也不能匹配,则不执行下面的操作
//                    if (!StringUtil.isEmpty(conditionRequest.getName())){
//                        Wrapper<AcFunc> acFuncWrapper = new EntityWrapper<AcFunc>();
//                        acFuncWrapper.eq(AcFunc.COLUMN_GUID,acFunc.getGuid());
//                        acFuncWrapper.like(AcFunc.COLUMN_FUNC_NAME,conditionRequest.getName());
//
//                        AcFunc acFuncQue = acFuncService.selectOne(acFuncWrapper);
//                        if (null == acFuncQue){
//                            continue;
//                        }
//                    }
//                }
//
//                //处理完功能下的行为,处理该功能.判断该功能是否已分配
//                Wrapper<AcOperatorFunc> acOperatorFuncWrapper = new EntityWrapper<AcOperatorFunc>();
//                acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_OPERATOR,conditionRequest.getOperatorId());
//                acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
//                acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_FUNC,acFunc.getGuid());
//
//                AcOperatorFunc acOperatorFunc = selectOne(acOperatorFuncWrapper);
//                AcFuncExit acFuncExit = new AcFuncExit();
//                if (null == acOperatorFunc){
//                    acFuncExit = new AcFuncExit(acFunc,"true",exitBehaveList);
//                }
//                if (null != acOperatorFunc){
//                    acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList);
//                }
//
//                exitList.add(acFuncExit);acFuncExit
            }
        }
        return exitList;
    }

    /**
     * 查询应用下的所有功能 和 操作员和应用下的所有功能
     */
    @Override
    public AcRoleFuncQueRequest<AcFunc> queryFuncByOperator(AcOperatorFuncQueConditionRequest conditionRequest) throws AcManagementException {

        //判断应用id是否为空
        if (StringUtil.isEmpty(conditionRequest.getAppId())){
            throw new AcManagementException(AcExceptionCodes.APPID_ISNULL_WHRN_QUERY_ROLE_FUNC,wrap("应用的id不能为空"));
        }

        //查询应用下的所有功能
        Wrapper<AcFunc> funcWrapper = new EntityWrapper<AcFunc>();
        funcWrapper.eq(AcFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
        funcWrapper.eq(AcFunc.COLUMN_FUNC_TYPE, FuncType.FUNCTION);

        List<AcFunc> allFuncs = acFuncService.selectList(funcWrapper);

        //查询操作员和应用下的功能
        List<AcFunc> olFuncs = this.baseMapper.queryFuncByOperator(conditionRequest.getOperatorId(),conditionRequest.getAppId());

        AcRoleFuncQueRequest<AcFunc> queRequest = new AcRoleFuncQueRequest<AcFunc>();
        queRequest.setAllData(allFuncs);
        queRequest.setOldData(olFuncs);

        return queRequest;
    }

    /**
     * 查询功能下的所有行为 和 操作员和功能下的所有行为
     */
    @Override
    public AcRoleFuncQueRequest<AcFunc> queryBehaveByOperator(AcOperatorFuncQueConditionRequest conditionRequest) throws AcManagementException {

        //判断应用id是否为空
        if (StringUtil.isEmpty(conditionRequest.getAppId())){
            throw new AcManagementException(AcExceptionCodes.APPID_ISNULL_WHRN_QUERY_ROLE_FUNC,wrap("应用的id不能为空"));
        }

        //判断功能id是否为空
        if (StringUtil.isEmpty(conditionRequest.getFuncId())){
            throw new AcManagementException(AcExceptionCodes.FUNCID_ISNULL_WHRN_QUERY_ROLE_FUNC,wrap("功能的id不能为空"));
        }

        //查询功能下的所有行为
        Wrapper<AcFunc> funcWrapper = new EntityWrapper<AcFunc>();
        funcWrapper.eq(AcFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
        funcWrapper.eq(AcFunc.COLUMN_GUID_FUNC,conditionRequest.getFuncId());
        funcWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.BEHAVE);

        List<AcFunc> allBehaves= acFuncService.selectList(funcWrapper);

        //查询功能下已有的行为
        List<AcFunc> oldBehaves = this.baseMapper.queryBehaveByOperator(conditionRequest.getOperatorId(),conditionRequest
                .getAppId(),conditionRequest.getFuncId());

        AcRoleFuncQueRequest<AcFunc> queRequest = new AcRoleFuncQueRequest<AcFunc>();
        queRequest.setOldData(oldBehaves);
        queRequest.setAllData(allBehaves);

        return queRequest;
    }

    /**
     * 批量新增操作员_应用数据
     *
     * @throws AcManagementException
     */
    @Override
    public void batchAdd(AcOperatorBatchAddRequest batchAddRequest) throws AcManagementException {

        //查询该操作员下的已分配数据
        Wrapper<AcOperatorFunc> allOperatorFuncWrapper = new EntityWrapper<AcOperatorFunc>();
        allOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_OPERATOR,batchAddRequest.getOperatorId());
        List<AcOperatorFunc> assignedList = selectList(allOperatorFuncWrapper);

        //获取前端传来的数据
        List<AcOperatorFunc> NotTreatedList = allNewList(batchAddRequest);

        List<AcOperatorFunc> listNewAll = new ArrayList<AcOperatorFunc>();
        //添加新数据
        if (0 != NotTreatedList.size()){
            for (AcOperatorFunc acOperatorFunc : NotTreatedList){
                if (null != acOperatorFunc){
                    Wrapper<AcOperatorFunc> funcAddWrapper = new EntityWrapper<AcOperatorFunc>();
                    funcAddWrapper.eq(AcOperatorFunc.COLUMN_GUID_OPERATOR,acOperatorFunc.getGuidOperator());
                    funcAddWrapper.eq(AcOperatorFunc.COLUMN_GUID_APP,acOperatorFunc.getGuidApp());
                    funcAddWrapper.eq(AcOperatorFunc.COLUMN_GUID_FUNC,acOperatorFunc.getGuidFunc());

                    AcOperatorFunc acOperatorFuncQue = selectOne(funcAddWrapper);
                    if (null == acOperatorFuncQue){
                        AcOperatorFunc acOperatorFuncAdd = new AcOperatorFunc();
                        acOperatorFuncAdd.setGuidOperator(acOperatorFunc.getGuidOperator());
                        acOperatorFuncAdd.setGuidFunc(acOperatorFunc.getGuidFunc());
                        acOperatorFuncAdd.setGuidApp(acOperatorFunc.getGuidApp());
                        acOperatorFuncAdd.setAuthType(AcAuthType.PERMIT);

                        insert(acOperatorFuncAdd);
                        AcOperatorFunc acOperatorFunc1 = selectOne(funcAddWrapper);
                        listNewAll.add(acOperatorFunc1);
                    }else {
                        listNewAll.add(acOperatorFuncQue);
                    }
                }
            }
        }

        //找出原数据有,现数据中没有的数据,用于删除使用
        assignedList.removeAll(listNewAll);
        //删除多于的数据
        if (0 != assignedList.size()){
            for (AcOperatorFunc acOperatorFunc : assignedList){
                if (null != acOperatorFunc){
                    deleteOperatorFunc(acOperatorFunc.getGuid());
                }
            }
        }
    }


    /** 整理出前端传来的数据 */
    private List<AcOperatorFunc> allNewList(AcOperatorBatchAddRequest batchAddRequest) {

        //处理前端传来的数据,存在list中
        List<AcOperatorFunc> NotTreatedList = new ArrayList<AcOperatorFunc>();
        if ( null != batchAddRequest.getBehaveIdList()){
            for (String behaveId : batchAddRequest.getBehaveIdList()){
                if (!StringUtil.isEmpty(behaveId)){
                    //查询该行为
                    AcFunc acBehave = acFuncService.selectById(behaveId);
                    if (null != acBehave){
                        //将该条数据的功能 存储在acOperatorFunc对象中
                        AcOperatorFunc acOperatorFunc = new AcOperatorFunc();
                        acOperatorFunc.setGuidOperator(batchAddRequest.getOperatorId());
                        acOperatorFunc.setGuidApp(acBehave.getGuidApp());
                        acOperatorFunc.setGuidFunc(acBehave.getGuidFunc());

                        NotTreatedList.add(acOperatorFunc);

                        //将该对象存储在acOperatorBehave对象中
                        AcOperatorFunc acOperatorBehave = new AcOperatorFunc();
                        acOperatorBehave.setGuidOperator(batchAddRequest.getOperatorId());
                        acOperatorBehave.setGuidApp(acBehave.getGuidApp());
                        acOperatorBehave.setGuidFunc(behaveId);

                        NotTreatedList.add(acOperatorBehave);
                    }
                }
            }
        }

        //去除list中重复的数据
        if (0 != NotTreatedList.size()){
            HashSet<AcOperatorFunc> hashSet = new HashSet<AcOperatorFunc>(NotTreatedList);
            NotTreatedList.clear();
            NotTreatedList.addAll(hashSet);
        }

        return NotTreatedList;
    }


    /** 整理出前端传来的数据 */
    private List<AcOperatorFunc> findAllData(AcOperatorBatchAddRequest batchAddRequest){
        //查询该角色现有数据
        List<AcOperatorFunc> listsNew = new ArrayList<AcOperatorFunc>();
        if (0 != batchAddRequest.getBatchData().size()){
            //遍历map,获取应用id
            for(String  appId : batchAddRequest.getBatchData().keySet()){
                if (!StringUtil.isEmpty(appId)){
                    //获得
                    OperatorRoleAdd operatorRoleAdd = batchAddRequest.getBatchData().get(appId);
                    if (null != operatorRoleAdd){
                        for (String funcId : operatorRoleAdd.getFuncData().keySet()){
                            if (!StringUtil.isEmpty(funcId)){
                                AcOperatorFunc acOperatorFunc1 = new AcOperatorFunc();
                                for (String behaveId : operatorRoleAdd.getFuncData().get(funcId)){
                                    if (!StringUtil.isEmpty(behaveId)){
                                        AcOperatorFunc acOperatorFunc2 = new AcOperatorFunc();
                                        acOperatorFunc2.setGuidApp(appId);
                                        acOperatorFunc2.setGuidOperator(batchAddRequest.getOperatorId());
                                        acOperatorFunc2.setGuidFunc(behaveId);

                                        listsNew.add(acOperatorFunc2);
                                    }
                                }

                                acOperatorFunc1.setGuidApp(appId);
                                acOperatorFunc1.setGuidOperator(batchAddRequest.getOperatorId());
                                acOperatorFunc1.setGuidFunc(funcId);
                                listsNew.add(acOperatorFunc1);
                            }
                        }
                    }
                }
            }
        }
        return listsNew;
    }

    /**
     * 硬删除
     */
    @Override
    public void deleteOperatorFunc(String id) throws AcManagementException {
        this.baseMapper.deleteOperatorFunc(id);
    }

    /**
     * 批量新增和删除
     *
     * @param operatorGuid 操作员GUID
     * @param dataList     批量新增和删除的数组
     * @throws AcManagementException
     */
    @Override
    public void batchAddAndDelete(String operatorGuid, List<AcOpertorAddDelete> dataList) throws AcManagementException {

        if (0 != dataList.size()){
            for (AcOpertorAddDelete acOpertor : dataList){
                //判断数据是否来自于角色,如果来自角色则进行特别禁止
                if ("Y".equals(acOpertor.getFromRole()) && "true".equals(acOpertor.getExit())){
                    AcOperatorFunc acOperatorFuncAdd = new AcOperatorFunc();
                    acOperatorFuncAdd.setGuidOperator(operatorGuid);
                    acOperatorFuncAdd.setGuidFunc(acOpertor.getFuncGuid());
                    acOperatorFuncAdd.setGuidApp(acOpertor.getAppGuid());
                    acOperatorFuncAdd.setAuthType(AcAuthType.FORBID);

                    insert(acOperatorFuncAdd);
                    continue;
                }
                if ("N".equals(acOpertor.getFromRole()) && "false".equals(acOpertor.getExit())){
                    AcOperatorFunc acOperatorFuncAdd = new AcOperatorFunc();
                    acOperatorFuncAdd.setGuidOperator(operatorGuid);
                    acOperatorFuncAdd.setGuidFunc(acOpertor.getFuncGuid());
                    acOperatorFuncAdd.setGuidApp(acOpertor.getAppGuid());
                    acOperatorFuncAdd.setAuthType(AcAuthType.PERMIT);

                    insert(acOperatorFuncAdd);
                    addFuncByBehave(operatorGuid,acOpertor.getFuncGuid(),acOpertor.getAppGuid());
                    continue;
                }
                //删除不是来自角色的特别允许的数据和来自角色的特别禁止的数据
                boolean candel = ("N".equals(acOpertor.getFromRole()) && "true".equals(acOpertor.getExit())) || ("Y".equals(acOpertor.getFromRole()) && "false".equals(acOpertor.getExit()));
                if (candel){
                    Wrapper<AcOperatorFunc> funcWrapper = new EntityWrapper<>();
                    funcWrapper.eq(AcOperatorFunc.COLUMN_GUID_OPERATOR,operatorGuid);
                    funcWrapper.eq(AcOperatorFunc.COLUMN_GUID_APP,acOpertor.getAppGuid());
                    funcWrapper.eq(AcOperatorFunc.COLUMN_GUID_FUNC,acOpertor.getFuncGuid());
                    List<AcOperatorFunc> acOperatorFuncs = selectList(funcWrapper);
                    if (0 != acOperatorFuncs.size()){
                        deleteOperatorFunc(acOperatorFuncs.get(0).getGuid());
                    }
                }
            }
        }
    }

    private void addFuncByBehave(String operatorId, String funcId, String appId){

        AcFunc behave = acFuncService.selectById(funcId);
        if (null != behave){
            if (FuncType.BEHAVE.toString().equals(behave.getFuncType().toString()) && StringUtil.isNotEmpty(behave
                    .getGuidFunc())){
                AcFunc acFunc = acFuncService.selectById(behave.getGuidFunc());

                Wrapper<AcOperatorFunc> wrapper = new EntityWrapper<>();
                wrapper.eq(AcOperatorFunc.COLUMN_GUID_OPERATOR,operatorId);
                wrapper.eq(AcOperatorFunc.COLUMN_GUID_APP,appId);
                wrapper.eq(AcOperatorFunc.COLUMN_GUID_FUNC,acFunc.getGuid());
                List<AcOperatorFunc> list = selectList(wrapper);
                if (0 == list.size()){
                    AcOperatorFunc acOperatorFuncAdd = new AcOperatorFunc();
                    acOperatorFuncAdd.setGuidOperator(operatorId);
                    acOperatorFuncAdd.setGuidFunc(acFunc.getGuid());
                    acOperatorFuncAdd.setGuidApp(appId);
                    acOperatorFuncAdd.setAuthType(AcAuthType.PERMIT);
                    insert(acOperatorFuncAdd);
                }
            }
        }
    }


    /**
     * 过滤查询操作员下功能行为
     *
     * @param operatorId 操作员GUID
     * @param name       功能行为名称
     * @return AcRoleFuncQueRequest<AcAppRoleDetail>
     * @throws AcManagementException
     */
    @Override
    public AcRoleFuncQueRequest<AcAppRoleDetail> getFilterFuncBehaveByRole(String operatorId, String name) throws AcManagementException {

        AcRoleFuncQueRequest<AcAppRoleDetail> acOperatorFuncQueRequest = new AcRoleFuncQueRequest<>();

        if (StringUtil.isEmpty(name)){
            //查询出操作员下所有的应用功能
            AcRoleFuncQueRequest<AcApp> acAppAcRoleFuncQueRequest = queryAppByOperator(operatorId);
            List<AcAppRoleDetail> allData = getAppTree(acAppAcRoleFuncQueRequest.getAllData(),operatorId,name);
            List<AcAppRoleDetail> oldData = getAppTree(acAppAcRoleFuncQueRequest.getOldData(),operatorId,name);

            acOperatorFuncQueRequest.setAllData(allData);
            acOperatorFuncQueRequest.setOldData(oldData);
        }else {
            //模糊匹配出所有的功能
            List<AcFuncExit> funcExitList = new ArrayList<>();

            //查询应用下的所有功能
            Wrapper<AcFunc> allFuncWrapper = new EntityWrapper<AcFunc>();
            allFuncWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.FUNCTION);
            allFuncWrapper.like(AcFunc.COLUMN_FUNC_NAME,name);
            List<AcFunc> allFuncList = acFuncService.selectList(allFuncWrapper);

            for (AcFunc acFunc : allFuncList){
                if (null != acFunc){
                    //如果功能存在,获取功能下的所有行为
                    Wrapper<AcFunc> allBehaveWrapper = new EntityWrapper<>();
                    allBehaveWrapper.eq(AcFunc.COLUMN_GUID_FUNC,acFunc.getGuid());
                    allBehaveWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.BEHAVE);
                    List<AcFunc> allBehaveList = acFuncService.selectList(allBehaveWrapper);

                    //查询角色下的行为,添加模糊匹配
                    List<AcFunc> roleBehaveList = this.baseMapper.queryBehaveInRoleFunc(operatorId,acFunc.getGuid());

                    //查询操作员下的行为,添加模糊匹配,特别允许
                    List<AcFunc> operatorRemitBehaveList  = this.baseMapper.queryBehaveRemitInOperatorFunc(operatorId, acFunc.getGuid());

                    //查询操作员下的行为,添加模糊匹配,特别禁止
                    List<AcFunc> operatorForbidBehaveList = this.baseMapper.queryBehaveForbidInOperatorFunc(operatorId, acFunc.getGuid());


                    //去除掉特别允许中为角色行为的数据
                    if (0 != operatorRemitBehaveList.size()){
                        operatorRemitBehaveList.removeAll(roleBehaveList);
                    }
                    //去除角色行为中特别禁止的数据
                    if (0 != roleBehaveList.size()){
                        roleBehaveList.removeAll(operatorForbidBehaveList);
                    }
                    //去除已经被分配的行为,找出所有未被分配的行为
                    if (0 != allBehaveList.size()){
                        allBehaveList.removeAll(roleBehaveList);
                        allBehaveList.removeAll(operatorForbidBehaveList);
                        allBehaveList.removeAll(operatorRemitBehaveList);
                    }


                    /**
                     * 用于存放该功能下的所有行为
                     * exit标识出 已分配的为"false",未分配的为"true"
                     * fromRole标志出 来自角色:Y,未来自角色:N
                     * authType表指出 特别允许:remit,特别禁止:forbid
                     */
                    List<AcFuncExit> exitBehaveList = new ArrayList<>();
                    if (0 != allBehaveList.size() || 0 != roleBehaveList.size() || 0 != operatorForbidBehaveList.size()
                            || 0 != operatorRemitBehaveList.size() ){
                        for (AcFunc behave: allBehaveList){
                            if (null != behave){
                                AcFuncExit acBehaveExit = new AcFuncExit(behave,"true",null,"N",null);
                                exitBehaveList.add(acBehaveExit);
                            }
                        }
                        for (AcFunc behave: roleBehaveList){
                            if (null != behave){
                                AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null,"Y",null);
                                exitBehaveList.add(acBehaveExit);
                            }
                        }
                        for (AcFunc behave : operatorForbidBehaveList){
                            if (null != behave){
                                //特别禁止的都是之前来自于角色的,所有fromRole是Y
                                AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null,"Y","forbid");
                                exitBehaveList.add(acBehaveExit);
                            }
                        }
                        for (AcFunc behave : operatorRemitBehaveList){
                            if (null != behave){
                                AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null,"N","remit");
                                exitBehaveList.add(acBehaveExit);
                            }
                        }
                    }else {
                        //如果行为为空,并且功能也不能匹配,则不执行下面的操作
                        if (!StringUtil.isEmpty(name)){
                            Wrapper<AcFunc> acFuncWrapper = new EntityWrapper<AcFunc>();
                            acFuncWrapper.eq(AcFunc.COLUMN_GUID,acFunc.getGuid());
                            AcFunc acFuncQue = acFuncService.selectOne(acFuncWrapper);
                            if (null == acFuncQue){
                                continue;
                            }
                        }
                    }

                    //判断功能是否来自于角色
                    Wrapper<AcOperatorRole> operatorRoleWrapper = new EntityWrapper<>();
                    operatorRoleWrapper.eq(AcOperatorRole.COLUMN_GUID_OPERATOR,operatorId);
                    List<AcOperatorRole> acOperatorRoles = acOperatorRoleService.selectList(operatorRoleWrapper);
                    AcFuncExit acFuncExit = null;
                    if (0 != acOperatorRoles.size()){
                        for (AcOperatorRole acOperatorRole : acOperatorRoles){
                            if (null != acOperatorRole){
                                Wrapper<AcRoleFunc> roleFuncWrapper = new EntityWrapper<>();
                                roleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,acOperatorRole.getGuidRole());
                                roleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_FUNC,acFunc.getGuid());

                                AcRoleFunc acRoleFunc = acRoleFuncService.selectOne(roleFuncWrapper);
                                if (null != acRoleFunc){
                                    acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"Y",null);
                                }
                            }
                        }
                    }
                    if (0 == acOperatorRoles.size() || null == acFuncExit){
                        //处理完功能下的行为,处理该功能.判断该功能是否已分配
                        Wrapper<AcOperatorFunc> acOperatorFuncWrapper = new EntityWrapper<>();
                        acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_OPERATOR,operatorId);
                        acOperatorFuncWrapper.eq(AcOperatorFunc.COLUMN_GUID_FUNC,acFunc.getGuid());

                        AcOperatorFunc acOperatorFunc = selectOne(acOperatorFuncWrapper);
                        if (null == acOperatorFunc){
                            acFuncExit = new AcFuncExit(acFunc,"true",exitBehaveList,"N",null);
                        }
                        if (null != acOperatorFunc){
                            if (acOperatorFunc.getAuthType().toString().equals(AcAuthType.PERMIT.toString())){
                                acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"N","remit");
                            }else {
                                //功能被特别禁止,则功能下的所有行为也被特别禁止
                                for (AcFuncExit acBehaveExit : exitBehaveList){
                                    if (null != acBehaveExit){
                                        acBehaveExit.setAuthType("forbid");
                                    }
                                }
                                acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"N","forbid");
                            }
                        }
                    }
                    funcExitList.add(acFuncExit);
                }
            }

            //将到guidApp相同的放在一起
            if (0 == funcExitList.size()){
                return null;
            }

            Map<String,List<AcFuncExit>> map = new HashMap<>();
            for (AcFuncExit acFuncExit : funcExitList){
                if (null != acFuncExit){
                    if (map.containsKey(acFuncExit.getGuidApp())){
                        map.get(acFuncExit.getGuidApp()).add(acFuncExit);
                    }else {
                        List<AcFuncExit> list = new ArrayList<>();
                        list.add(acFuncExit);
                        map.put(acFuncExit.getGuidApp(),list);
                    }
                }
            }

            if (0 != map.size()){
                List<AcAppRoleDetail> allData = new ArrayList<>();
                for (String appGuid : map.keySet()){
                    AcApp acApp = acAppService.selectById(appGuid);
                    AcAppRoleDetail acAppRoleDetail = new AcAppRoleDetail(acApp,map.get(appGuid));
                    allData.add(acAppRoleDetail);
                }
                acOperatorFuncQueRequest.setAllData(allData);
            }
        }

        return acOperatorFuncQueRequest;
    }


    private List<AcAppRoleDetail> getAppTree(List<AcApp> list,String operatorId,String name){

        List<AcAppRoleDetail> appRoleDetailList = new ArrayList<>();

        if (0 == list.size()){
            return appRoleDetailList;
        }

        for (AcApp acApp : list){
            if (null != acApp){
                List<AcFuncExit> funcExitlist = queryFuncBehaveByOperatorApp(operatorId,acApp.getGuid(),name);
                AcAppRoleDetail acAppRoleDetail = new AcAppRoleDetail(acApp,funcExitlist);
                appRoleDetailList.add(acAppRoleDetail);
            }
        }

        return appRoleDetailList;
    }
}

